package app.cn.com.school.activity

import android.os.Bundle
import android.support.v4.app.FragmentTransaction
import android.view.Gravity
import android.widget.RadioGroup
import android.widget.Toast
import app.cn.com.school.R
import app.cn.com.school.databinding.ActivityMainBinding
import app.cn.com.school.fragment.*
import app.cn.com.school.tools.AppManager
import app.cn.com.school.tools.XPermissionUtils
import kotlin.concurrent.thread

class MainActivity : BaseActivity<ActivityMainBinding>(), RadioGroup.OnCheckedChangeListener {
    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        var transaction = supportFragmentManager.beginTransaction()
        hideAllFragment(transaction)
        when (checkedId) {
            R.id.tab_menu_home -> if (homeFg == null) {
                homeFg = HomeFragment.newInstance("", "")
                transaction.add(R.id.frame, homeFg)
            } else {
                transaction.show(homeFg)
            }
            R.id.tab_menu_welfare -> if (welfareFg == null) {
                welfareFg = WelfareFragment.newInstance("", "")
                transaction.add(R.id.frame, welfareFg)
            } else {
                transaction.show(welfareFg)
            }
            R.id.tab_menu_used -> if (usedFg == null) {
                usedFg = UsedFragment.newInstance("", "")
                transaction.add(R.id.frame, usedFg)
            } else {
                transaction.show(usedFg)
            }
            R.id.tab_menu_praise -> if (praiseFg == null) {
                praiseFg = PraiseFragment.newInstance("", "")
                transaction.add(R.id.frame, praiseFg)
            } else {
                transaction.show(praiseFg)
            }
            R.id.tab_menu_mine -> if (mineFg == null) {
                mineFg = MineFragment.newInstance("", "")
                transaction.add(R.id.frame, mineFg)
            } else {
                transaction.show(mineFg)
            }
        }
        transaction.commit()

    }

    override fun initBindingVar() {
    }

    override fun initTitle() {
    }

    var homeFg: HomeFragment? = null
    var mineFg: MineFragment? = null
    var usedFg: UsedFragment? = null
    var welfareFg: WelfareFragment? = null
    var praiseFg: PraiseFragment? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_main)
//        LocationTools().set(this@MainActivity) //定位
        /**
         * 另外的写法
         * */
        initBottom()
    }

    private fun initBottom() {
        binding.rdGroup.setOnCheckedChangeListener(this)
        binding.tabMenuHome.isChecked = true
        if (homeFg == null) {
            homeFg = HomeFragment.newInstance("", "")
            supportFragmentManager.beginTransaction().add(R.id.frame, homeFg).commit()
        } else {
            supportFragmentManager.beginTransaction().show(homeFg).commit()
        }
    }

    private fun hideAllFragment(transaction: FragmentTransaction) {

        if (homeFg != null) {
            transaction.hide(homeFg)
        }
        if (mineFg != null) {
            transaction.hide(mineFg)
        }
        if (usedFg != null) {
            transaction.hide(usedFg)
        }
        if (welfareFg != null) {
            transaction.hide(welfareFg)
        }
        if (praiseFg != null) {
            transaction.hide(praiseFg)
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        AppManager.getAppManager().finishActivity(this)
    }

    @Volatile
    var backTime = 0

    override fun onBackPressed() {
        if (backTime == 0) {
            val t = Toast.makeText(this, "再按一次退出App", Toast.LENGTH_SHORT)
            t.setGravity(Gravity.CENTER, 0, 0)
            t.show()
            backTime = 1
            thread {
                Thread.sleep(1000)
                backTime = 0
            }
        } else {
            super.onBackPressed()
        }
    }

    override fun onResume() {
        super.onResume()
        AppManager.getAppManager().addActivity(this)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        XPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

}
